$(function () {
    UserLogin();
    UserPermission();
    LoginLive();

})

// 不能设置cookie 存在账号密码泄露风险
// function RemeberMe() {
//     if ($.cookie('username') && $.cookie('password')){
//         $('#login_user_name').val($.cookie('username'))
//         $('#login_password').val($.cookie('password'))
//         $('#remember_me').attr('checked',true);
//     }
// }

//登陆状态跳转到个人详情页面
function LoginLive() {
    if ((localStorage.token && localStorage.user_id && localStorage.username) || (sessionStorage.token && sessionStorage.user_id && sessionStorage.username)){
        location.href = './personal_center.html';
    }
}


// 检测用户名是否存在
function UserLogin() {
    $('#login_in').click(function () {
        $('#login_in_error, #login_user_name_error, #login_password_error, #login_in_error').html('')
        var username = $('#login_user_name').val()
        var password = $('#login_password').val()
        var remember_me = $('#remember_me').prop("checked");
        if (!username){
            $('#login_user_name_error').html('用户名不能为空！')
        }
        if (!password){
            $('#login_password_error').html('密码不能为空！')
        }

        // console.log(remember_me, username)
        params = {
            'username': username,
            'password': password
        };

        $.ajax({
            url: host + "/authorizations/",
            method: "post",
            data: JSON.stringify(params),
            contentType: "application/json",
            xhrFields: {
                withCredentials: true
            },
            success: function (resp) {
                var res_data = eval(resp);
                // console.log(res_data)
                if (remember_me == true){
                    // 记住登录
                    sessionStorage.clear();
                    localStorage.token = res_data.token;
                    localStorage.user_id = res_data.user_id;
                    localStorage.username = res_data.username;
                    localStorage.nickname = res_data.nickname;
                    localStorage.sex = res_data.sex;
                    localStorage.email = res_data.email;
                    localStorage.address = res_data.address;
                    // console.log(localStorage.token, sessionStorage.token);
                    // 不能设置cookie 存在账号密码泄露风险
                    // $.cookie("username", username , { expires: 7, path: '/' });
                    // $.cookie("password", password , { expires: 7, path: '/' });
                    location.href = './personal_center.html';

                }else {
                    // 未记住登录
                    localStorage.clear();
                    sessionStorage.token = res_data.token;
                    sessionStorage.user_id = res_data.user_id;
                    sessionStorage.username = res_data.username;
                    sessionStorage.nickname = res_data.nickname;
                    sessionStorage.sex = res_data.sex;
                    sessionStorage.email = res_data.email;
                    sessionStorage.address = res_data.address;
                    location.href = './personal_center.html'
                    // console.log(sessionStorage.username, sessionStorage.user_id, sessionStorage.nickname);
                }

            },
            error: function (resp) {
                // resp_data = JSON.parse(resp.responseText);  // 可以取到错误信息
                // console.log(resp.status)
                if (resp.status == 400){
                    $('#login_in_error').html('用户名密码错误！')
                }else {
                    $('#login_in_error').html('服务器错误！')
                }
            }
        })

    })

}

